import io
import segno 

import frappe
from werkzeug.wrappers import Response

@frappe.whitelist()
def get_code():
  pBatch = frappe.request.args.get('batch', default='', type=str)
  pQuantity = frappe.request.args.get('quantity', default=0, type=int)
  pDate = frappe.request.args.get('date', default='', type=str)
  pScale = frappe.request.args.get('scale', default=4, type=int)
  pBorder = frappe.request.args.get('border', default=2, type=int)

  temp = "批次号:" + str(pBatch)  + " 数量:" + str(pQuantity) +  " 生产日期:" + str(pDate)

  qrcode = segno.make(temp)
  buffer = io.BytesIO()
  qrcode.save(buffer, kind='png', scale=pScale, border=pBorder)
  buffer.seek(0)

  response = Response()
  response.mimetype = 'image/png'
  response.data = buffer.read()
  return response
